package ui;

import entity.User;
import exception.DuplicateUsernameException;
import factory.ObjectFactory;
import service.UserService;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Enumeration;

public class RegistForm {
    private JPanel panel;
    private JLabel title;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JLabel sexLabel;
    private JLabel eduLabel;
    private JLabel hobbiesLabel;
    private JLabel descriptionLabel;

    private JTextField usernameField;
    private JPasswordField passwordField;
    private JRadioButton sexMButton;
    private JRadioButton sexWButton;
    private ButtonGroup sexButtonGroup;
    private JComboBox eduComboBox;
    private JCheckBox[] hobbiesCheckBoxes;
    private JTextArea descriptionArea;

    private JLabel messageLabel;

    private JButton registButton;

    public RegistForm() {
        panel=new JPanel();
        title=new JLabel("用户注册");
        usernameLabel=new JLabel("账号：");
        passwordLabel=new JLabel("密码：");
        sexLabel=new JLabel("性别：");
        eduLabel=new JLabel("学历：");
        hobbiesLabel=new JLabel("爱好：");
        descriptionLabel=new JLabel("简介：");

        usernameField=new JTextField(20);
        passwordField=new JPasswordField(20);
        sexMButton=new JRadioButton("男");
        sexWButton=new JRadioButton("女");
        sexButtonGroup=new ButtonGroup();
        sexButtonGroup.add(sexMButton);
        sexButtonGroup.add(sexWButton);
        eduComboBox=new JComboBox();
        String[] eduItems={"---请选择---","高中","大专","本科"};
        for (String item : eduItems) {
            eduComboBox.addItem(item);
        }
        String[] hobbiesItems={"吃","玩","睡"};
        hobbiesCheckBoxes=new JCheckBox[hobbiesItems.length];
        for (int i = 0; i < hobbiesItems.length; i++) {
            hobbiesCheckBoxes[i]=new JCheckBox(hobbiesItems[i]);
        }
        descriptionArea=new JTextArea(5,20);


        messageLabel=new JLabel(" ");

        registButton=new JButton("注册");
    }

    private void init(){
        panel.setLayout(new BorderLayout(3,3));

        JPanel northPanel=new JPanel();
        northPanel.add(title);

        JPanel centerPanel=new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));

        JPanel usernamePanel=new JPanel();
        usernamePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        usernamePanel.add(Box.createRigidArea(new Dimension(10,0)));
        usernamePanel.add(usernameLabel);
        usernamePanel.add(usernameField);

        JPanel passwordPanel=new JPanel();
        passwordPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        passwordPanel.add(Box.createRigidArea(new Dimension(10,0)));
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);

        JPanel sexPanel=new JPanel();
        sexPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        sexPanel.add(Box.createRigidArea(new Dimension(10,0)));
        sexPanel.add(sexLabel);
        sexPanel.add(sexMButton);
        sexPanel.add(sexWButton);

        JPanel eduPanel=new JPanel();
        eduPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        eduPanel.add(Box.createRigidArea(new Dimension(10,0)));
        eduPanel.add(eduLabel);
        eduPanel.add(eduComboBox);

        JPanel hobbiesPanel=new JPanel();
        hobbiesPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        hobbiesPanel.add(Box.createRigidArea(new Dimension(10,0)));
        hobbiesPanel.add(hobbiesLabel);
        for (JCheckBox hobbiesCheckBox : hobbiesCheckBoxes) {
            hobbiesPanel.add(hobbiesCheckBox);
        }
        JPanel descriptionPanel=new JPanel();
        descriptionPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        descriptionPanel.add(Box.createRigidArea(new Dimension(10,0)));
        descriptionPanel.add(descriptionLabel);
        JScrollPane jsp=new JScrollPane(descriptionArea);
        descriptionPanel.add(jsp);

        JPanel messagePanel=new JPanel();
        messagePanel.add(messageLabel);



        centerPanel.add(Box.createRigidArea(new Dimension(0,10)));
        centerPanel.add(usernamePanel);
        centerPanel.add(passwordPanel);
        centerPanel.add(sexPanel);
        centerPanel.add(eduPanel);
        centerPanel.add(hobbiesPanel);
        centerPanel.add(descriptionPanel);
        centerPanel.add(messagePanel);



        JPanel southPanel=new JPanel();
        southPanel.add(registButton);


        panel.add(northPanel,BorderLayout.NORTH);
        panel.add(centerPanel,BorderLayout.CENTER);
        panel.add(southPanel,BorderLayout.SOUTH);


    }

    private void setStyle(){
        title.setFont(new Font("宋体",Font.BOLD,16));
    }

    private void addEventHandler(){
        registButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username=usernameField.getText();
                String password=new String(passwordField.getPassword());
                String sex=null;
                Enumeration<AbstractButton> buttons=sexButtonGroup.getElements();
                while(buttons.hasMoreElements()){
                    AbstractButton button= buttons.nextElement();
                    if(button.isSelected()){
                        sex=button.getText();
                    }
                }
                String edu=eduComboBox.getSelectedIndex()==0?null: (String) eduComboBox.getSelectedItem();
                String[] hobbies=Arrays.stream(hobbiesCheckBoxes)
                                       .filter(JCheckBox::isSelected)
                                       .map(JCheckBox::getText)
                                       .toArray(String[]::new);
                String description=descriptionArea.getText();


                System.out.println("username = " + username);
                System.out.println("password = " + password);
                System.out.println("sex = " + sex);
                System.out.println("edu = " + edu);
                System.out.println("hobbies = " + Arrays.toString(hobbies));
                System.out.println("description = " + description);


                UserService userService= (UserService) ObjectFactory.getObject("userService");



                User user=new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setSex(sex);
                user.setEdu(edu);
                user.setHobbies(hobbies);
                user.setDescription(description);

                try {
                    userService.regist(user);

                    messageLabel.setForeground(Color.BLUE);
                    messageLabel.setText("注册成功");
                } catch (DuplicateUsernameException ex) {
                    messageLabel.setForeground(Color.RED);
                    usernameField.setForeground(Color.RED);
                    usernameField.setBackground(new Color(229, 210, 119));
                    messageLabel.setText(ex.getMessage());
                } catch (Exception ex){
                    messageLabel.setText("系统忙，稍后再试！");
                }


            }
        });

        usernameField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                usernameField.setForeground(Color.BLACK);
                usernameField.setBackground(Color.WHITE);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                usernameField.setForeground(Color.BLACK);
                usernameField.setBackground(Color.WHITE);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                usernameField.setForeground(Color.BLACK);
                usernameField.setBackground(Color.WHITE);
            }
        });
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
    }

    public JPanel getPanel() {
        return panel;
    }
}






















